package com.ruoyi.web.controller.net;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.net.domain.ElementIsis;
import com.ruoyi.net.domain.ElementIsisRoute;
import com.ruoyi.net.domain.NetElement;
import com.ruoyi.net.netconf.util.DeviceManager;
import com.ruoyi.net.service.IElementIsisRouteService;
import com.ruoyi.net.service.IElementIsisService;
import com.ruoyi.net.service.INetElementService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 网元-ISIS配置-ipv4地址族-路由重分发Controller
 * 
 * @author net
 * @date 2024-11-17
 */
@Api(tags = "网元-ISIS配置-ipv4地址族-路由重分发")
@RestController
@RequestMapping("/element/isis/route")
public class ElementIsisRouteController extends BaseController
{
    @Autowired
    private IElementIsisRouteService elementIsisRouteService;

    @Autowired
    private IElementIsisService elementIsisService;

    @Autowired
    private INetElementService netElementService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private DeviceManager deviceManager;

    /**
     * 查询网元-ISIS配置-ipv4地址族-路由重分发列表
     */
    @ApiOperation(value = "查询网元-ISIS配置-ipv4地址族-路由重分发列表", httpMethod = "GET")
    //    @PreAuthorize("@ss.hasPermi('system:route:list')")
    @GetMapping("/list")
    public TableDataInfo list(ElementIsisRoute elementIsisRoute)
    {
        startPage();
        List<ElementIsisRoute> list = elementIsisRouteService.selectElementIsisRouteList(elementIsisRoute);
        return getDataTable(list);
    }

    /**
     * 导出网元-ISIS配置-ipv4地址族-路由重分发列表
     */
    @ApiOperation(value = "导出网元-ISIS配置-ipv4地址族-路由重分发列表", httpMethod = "POST")
    //    @PreAuthorize("@ss.hasPermi('system:route:export')")
    @Log(title = "网元-ISIS配置-ipv4地址族-路由重分发", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElementIsisRoute elementIsisRoute)
    {
        List<ElementIsisRoute> list = elementIsisRouteService.selectElementIsisRouteList(elementIsisRoute);
        ExcelUtil<ElementIsisRoute> util = new ExcelUtil<ElementIsisRoute>(ElementIsisRoute.class);
        util.exportExcel(response, list, "网元-ISIS配置-ipv4地址族-路由重分发数据");
    }

    /**
     * 获取网元-ISIS配置-ipv4地址族-路由重分发详细信息
     */
    @ApiOperation(value = "获取网元-ISIS配置-ipv4地址族-路由重分发详细信息", httpMethod = "GET")
    //    @PreAuthorize("@ss.hasPermi('system:route:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(elementIsisRouteService.selectElementIsisRouteById(id));
    }

    /**
     * 新增网元-ISIS配置-ipv4地址族-路由重分发
     */
    @ApiOperation(value = "新增网元-ISIS配置-ipv4地址族-路由重分发", httpMethod = "POST")
    //    @PreAuthorize("@ss.hasPermi('system:route:add')")
    @Log(title = "网元-ISIS配置-ipv4地址族-路由重分发", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ElementIsisRoute elementIsisRoute)
    {
        sendXml(elementIsisRoute);
        return toAjax(elementIsisRouteService.insertElementIsisRoute(elementIsisRoute));
    }

    /**
     * 修改网元-ISIS配置-ipv4地址族-路由重分发
     */
    @ApiOperation(value = "修改网元-ISIS配置-ipv4地址族-路由重分发", httpMethod = "PUT")
    //    @PreAuthorize("@ss.hasPermi('system:route:edit')")
    @Log(title = "网元-ISIS配置-ipv4地址族-路由重分发", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ElementIsisRoute elementIsisRoute)
    {
        sendXml(elementIsisRoute);
        return toAjax(elementIsisRouteService.updateElementIsisRoute(elementIsisRoute));
    }

    /**
     * 删除网元-ISIS配置-ipv4地址族-路由重分发
     */
    @ApiOperation(value = "删除网元-ISIS配置-ipv4地址族-路由重分发", httpMethod = "DELETE")
    //    @PreAuthorize("@ss.hasPermi('system:route:remove')")
    @Log(title = "网元-ISIS配置-ipv4地址族-路由重分发", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(elementIsisRouteService.deleteElementIsisRouteByIds(ids));
    }


    private void sendXml(ElementIsisRoute entity) {
        // TODO: 2024/12/1  设备联调，添加并激活
        if (sysConfigService.selectDebugEnabled()) {
            NetElement netElement = netElementService.getCacheObject(entity.getElementId());
            deviceManager.getDevice(netElement.getIp());

            ElementIsis elementIsis = elementIsisService.selectElementIsisById(entity.getIsisId());
            String xml = "<isis xmlns=\"http://fiberhome.com/fhnw/yang/ipprotocol/routing/fos-isis\">" +
                    "    <isis-instances>" +
                    "      <isis-instance>" +
                    "        <instance-name>" + elementIsis.getIsis() + "</instance-name>" +
                    // -------start 路由重分发 ---------
                    "        <af>" +
                    "          <af-data>" +
                    "            <redistribution>" +
                    "              <protocol-name>"+entity.getRouteProtocol()+"</protocol-name>" +
                    "              <protocol-instance-id>"+entity.getProtocolDemo()+"</protocol-instance-id>" +
                    "              <protocol-instance-name>"+entity.getProtocolName()+"</protocol-instance-name>" +
//                    "              <router-id>"+entity.getRouteLevel()+"</router-id>" +
//                    "              <router-id>"+entity.getEndRouteLevelEnable()+"</router-id>" +
                    "              <levels>"+entity.getEndRouteLevel()+"</levels>" +
//                    "              <router-id>"+entity.getMeasureEnable()+"</router-id>" +
                    "              <metric-type>"+entity.getMeasureType()+"</metric-type>" +
                    "              <metric>"+entity.getMeasureValue()+"</metric>" +
                    "              <route-policy>" +
                    "                   <config>"+entity.getStrategyEnable()+"</config>" +
                    "                   <route-policy-name>"+entity.getStrategyName()+"</route-policy-name>" +
                    "              </route-policy>" +
                    "            </redistribution>" +
                    "          </af-data>" +
//                    "        </af>" +
                    // -------end  路由重分发 ---------
                    "      </isis-instance>" +
                    "    </isis-instances>" +
                    "  </isis>";
            System.out.println(xml);
            deviceManager.editConfig(xml, "merge");
        }
    }
}
